package com.example.myrecycleview.Adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.myrecycleview.Bean.TonyBean;
import com.example.myrecycleview.R;

import java.util.List;

public class TonyAdapter extends RecyclerView.Adapter<TonyAdapter.MyHolder> {


    private  List<TonyBean.ResultsBean> list;//存集合
    private  Context context;//上下文
    private ImageView imageView;

    public TonyAdapter(Context context, List<TonyBean.ResultsBean> list) {
        this.context=context;
        this.list=list;

    }

    @NonNull
    @Override
    public TonyAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        设置子布局
        View inflate = LayoutInflater.from(context).inflate(R.layout.recycleview_itm, parent, false);
//        MyHolder加载子布局
        MyHolder myHolder =new MyHolder(inflate);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull TonyAdapter.MyHolder holder, int position) {
//引用第三方库
        Glide.with(context)
                .load(list.get(position).getUrl())
                        .diskCacheStrategy(DiskCacheStrategy.ALL)              //缓存图片全尺寸
                        .placeholder(R.mipmap.ic_launcher_round)      //设置加载前的默认图片及加载失败的图片
                        .override(250,350)             //设置图片宽高
                        .into(imageView);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class MyHolder extends RecyclerView.ViewHolder {



        public MyHolder(@NonNull View itemView) {
            super(itemView);
//设置复用控件
            imageView = itemView.findViewById(R.id.tony_imge);
        }
    }
}
